package Properties;

import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;
import java.util.Properties;
import java.util.Set;

public class demo1 {
    //属性文件Properties的特点:键值对的形式、键不能重复、文件的结尾一般是properties，用处:存储有关系的键值对
    public static void main(String[] args) throws Exception {
        //写数据:键值对
        //先创建容器
        Properties properties = new Properties();
        //再写键值对数据
        properties.setProperty("小鸭","gagaga");
        properties.setProperty("小狗","wangwang");
        properties.setProperty("小羊","mianmian");

        //将容器中的数据写进文件中,保存进文件中
        properties.store(new FileWriter("D:\\software\\code\\2023_java\\2023_java_code\\code_24_4_28\\src\\Properties\\demo.properties"),"success");

        //将文件的数据读取到内存中
        properties.load(new FileReader("D:\\software\\code\\2023_java\\2023_java_code\\code_24_4_28\\src\\Properties\\demo.properties"));
        //将内存中的数据打印出来
        System.out.println(properties.getProperty("小鸭"));
        System.out.println(properties.getProperty("小羊"));
        System.out.println("========================");
        //遍历1
        properties.forEach((k,v)->{
            System.out.println(k + " :" + v);
        });
        System.out.println("========================");
        //遍历2
        Set<String> set = properties.stringPropertyNames();
        for (String s:set) {
            System.out.println(s + " :" + properties.getProperty(s));
        }
    }
}



